package com.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.service.EmpiPatientService;
import com.base.mapper.EmpiPatientRelMapper;
import com.base.mapper.PatientinfoBaseMapper;
import com.base.mapper.PatientinfoRawMapper;
import com.base.utils.ReflectionUtils;
import com.base.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author wnhuang
 * @Package com.base.api.impl
 * @date 2021-02-08 23:41
 */
@Service
public class EmpiPatientServiceImpl implements EmpiPatientService {

    @Autowired
    PatientinfoRawMapper patientinfoRawMapper;

    @Autowired
    EmpiPatientRelMapper empiPatientRelMapper;

    @Autowired
    PatientinfoBaseMapper patientinfoBaseMapper;


    /**
     * 获取相关记录
     *
     * @param empiid
     * @return
     */
    @Override
    public List<EmpiPatientRel> getPatientRelList(String empiid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("empiid",empiid);
        List<EmpiPatientRel> list = empiPatientRelMapper.selectList(wrapper);
        return list;
    }


    /**
     * 获取相关记录
     *
     * @param empiPatientRel
     * @return
     */
    @Override
    public List<EmpiPatientRel> getPatientRelList(EmpiPatientRel empiPatientRel) {
        Wrapper<EmpiPatientRel> wrapper = new QueryWrapper<>(empiPatientRel);
        List<EmpiPatientRel> list = empiPatientRelMapper.selectList(wrapper);
        return list;
    }

    /**
     * 获取比对记录
     *
     * @param iguid
     * @return
     */
    @Override
    public List<ComparePatientVo> ComparePatientRel(String iguid)  {
        List<PatientinfoRaw> list = patientinfoRawMapper.selectListByRelId(iguid);
        PatientinfoRaw val0 = list.get(0);
        PatientinfoRaw val1 = list.get(1);
        PatientinfoRaw val2;
        if (list.size() > 2) {
            val2 = list.get(2);
        } else {
            val2 = list.get(0);
        }
        String[] filedNames = ReflectionUtils.getFiledName(val0);
        List<ComparePatientVo> list1 = new ArrayList<>();
        for (int i = 0; i < filedNames.length; i++) {
            String fieldName = filedNames[i];
            ComparePatientVo comparePatientVo = new ComparePatientVo();
            comparePatientVo.setNowValue(ReflectionUtils.getFieldValueByName(fieldName,val0));
            comparePatientVo.setNewValue(ReflectionUtils.getFieldValueByName(fieldName,val1));
            comparePatientVo.setOldValue(ReflectionUtils.getFieldValueByName(fieldName,val2));
            comparePatientVo.setField(fieldName);
            comparePatientVo.setIndex(i);
            list1.add(comparePatientVo);
        }
        return list1;
    }


    /**
     * 合并记录
     *
     * @param guid
     */
    @Override
    @Transactional
    public void mergeRow(String guid) {
        EmpiPatientRel empiPatientRel = empiPatientRelMapper.selectById(guid);
        Assert.notNull(empiPatientRel, "合并记录时出错，在关系表中未找到相关记录，" + guid);
        if (empiPatientRel.getReltype() == 2) {
            empiPatientRelMapper.deletePatientBase(guid);
            PatientinfoBase patientinfoBase = new PatientinfoBase();
            patientinfoBase.setEmpiid(empiPatientRel.getEmpiid());
            patientinfoBase.setIssame((byte) 1);
            patientinfoBaseMapper.updateById(patientinfoBase);
            empiPatientRelMapper.deleteSameRow(guid);
            empiPatientRel.setReltype((byte) 1);
            empiPatientRel.setFieldsname("手工合并");
            empiPatientRel.setCreateat(new Date());
            empiPatientRelMapper.updateById(empiPatientRel);
        }
    }

    /**
     * 拆分记录
     *
     * @param guid
     */
    @Override
    @Transactional
    public void spiltRow(String guid) {
        EmpiPatientRel empiPatientRel = empiPatientRelMapper.selectById(guid);
        Assert.notNull(empiPatientRel, "拆分记录时出错，在关系表中未找到相关记录，" + guid);
        if (empiPatientRel.getReltype() == 1) {
            PatientinfoRaw patientinfoRaw = patientinfoRawMapper.selectById(empiPatientRel.getSeqno());
            PatientinfoBase patientinfoBase = new PatientinfoBase();
            BeanUtils.copyProperties(patientinfoRaw, patientinfoBase);
            String s = UUID.randomUUID().toString();
            patientinfoBase.setEmpiid(s);
            patientinfoBaseMapper.insert(patientinfoBase);
            empiPatientRel.setEmpiid(s);
            empiPatientRel.setCompareseqno(empiPatientRel.getSeqno());
            empiPatientRel.setCreateat(new Date());
            empiPatientRel.setFieldsname("手工拆分");
            empiPatientRelMapper.updateById(empiPatientRel);
        }
    }
}
